﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Browser;

namespace AdvancedStandingAndScholarship
{
    public class CookieHelper
    {
        public static string GetCookie(string key)
        {
            string[] cookies = HtmlPage.Document.Cookies.Split(';');

            foreach (string cookie in cookies)
            {
                string[] keyValue = cookie.Split('=');
                if (keyValue.Length == 2)
                {
                    string key1 = keyValue[0].ToLower().ToString().Trim();
                    string key2 = key.ToLower().ToString().Trim();
                    if (key1.Equals(key2) || key1 == key2)
                    {
                        return keyValue[1];
                    }
                    if (keyValue[0].ToString().ToLower().Equals(key.ToString().ToLower()))
                        return keyValue[1];
                }
            }
            return null;
        }


        public static void SetCookie(string key, string value)
        {
            DateTime expireDate = DateTime.Now.Add(TimeSpan.FromMinutes(15));
            string newCookie = String.Format("{0}={1};expires={2}", key, value, expireDate.ToString("R"));
            HtmlPage.Document.SetProperty("cookie", newCookie);
        }

    }
}
